import 'package:bloc/bloc.dart';
import 'package:today_app/bloc/calendar_today/calendar_today_model.dart';

import 'calendar_today_event.dart';
import 'calendar_today_state.dart';
import 'package:dio/dio.dart';

class CalendarTodayBloc extends Bloc<CalendarTodayEvent, CalendarTodayState> {
  CalendarTodayBloc() : super(CalendarTodayState().init()) {
    on<InitEvent>(_init);
  }

  @override
  void on<E extends CalendarTodayEvent>(EventHandler<E, CalendarTodayState> handler, {EventTransformer<E>? transformer}) {
    super.on(handler, transformer: transformer);
  }

  void _init(InitEvent event, Emitter<CalendarTodayState> emit) async {
    /**
     * {
        "reason":"Success",
        "result":{
        "data":{
        "animalsYear":"兔",
        "weekday":"星期六",
        "lunarYear":"癸卯年",
        "lunar":"二月初六",
        "year-month":"2023-2",
        "date":"2023-2-25",
        "suit":"签订合同.交易.纳财.买衣服.伐木.作梁",
        "avoid":"诸事不宜",
        "holiday":"",
        "desc":""
        }
        },
        "error_code":0
        }
     */
    // http://v.juhe.cn/calendar/day?date=2023-2-3&key=17a370865c0bb4bb428b248060b4dc4d
    Response response = await Dio().get("http://v.juhe.cn/calendar/day?date=2023-2-3&key=17a370865c0bb4bb428b248060b4dc4d");
    CalendarTodayModel model = CalendarTodayModel.fromJson(response.data);
    emit(state.setModel(model));
  }
}
